﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared;

namespace Shared.Method
{
    public class BobControl
    {

        public static readonly byte[] EEPROM_A2_00_7F = new byte[] { 0x50, 0x0, 0xf3, 0x0, 0x4b, 0x0, 0xf8, 0x0, 0x8c, 0xa0, 0x75, 0x30, 0x88, 0xb8, 0x79, 0x18, 0x7e, 0xf4, 0x0, 0x0, 0x6b, 0x6c, 0x0, 0x0, 0x9b, 0x82, 0x22, 0xd0, 0x8a, 0x99, 0x27, 0x10, 0x7, 0xcb, 0x0, 0xa, 0x6, 0x30, 0x0, 0xc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3f, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xa8, 0x1e, 0x7e, 0x81, 0x94, 0x10, 0x56, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x2, 0x0, 0x1, 0x40, 0x0, 0x0, 0x1, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x3 };
        public static readonly byte[] EEPROM_A2_T3_7F_FF = new byte[] { 0x03, 0xaa, 0x0, 0xc9, 0x40, 0xe3, 0x24, 0x2, 0x35, 0x0, 0xc0, 0x75, 0x0, 0x14, 0x60, 0x40, 0x88, 0xdc, 0xc8, 0x0, 0x0, 0x9, 0x9, 0x0, 0x0, 0x9, 0x28, 0x0, 0x0, 0x6, 0x28, 0x68, 0x10, 0x0, 0x0, 0xa8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xdc, 0x8c, 0x88, 0x1, 0xdc, 0x8c, 0x88, 0x1, 0xdc, 0x8c, 0x88, 0x1, 0x0, 0xf6, 0x94, 0x0, 0xcc, 0x0, 0x0, 0xd, 0x4a, 0x0, 0x0, 0x0, 0x5e, 0x0, 0x0, 0x0, 0x5e, 0x0, 0x0, 0x0, 0x5e, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xa2, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x29, 0x55, 0x0, 0x15, 0x2, 0x35, 0x1, 0x19, 0x3, 0x35, 0x1, 0x28, 0x0, 0x33, 0x9e, 0x1, 0x0, 0x0, 0x0, 0x0, 0x2, 0xff, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
        public static readonly byte[] EEPROM_A2_T4_7F_E8 = new byte[] { 0x04, 0xa, 0xc2, 0xb0, 0xac, 0x2b, 0xa, 0xc2, 0xb0, 0xac, 0x2b, 0xa, 0xc2, 0xb0, 0xb4, 0x2d, 0xb, 0x82, 0xe0, 0xc0, 0x30, 0xc, 0x3, 0x0, 0xc0, 0x31, 0xc, 0x43, 0x30, 0xcc, 0x34, 0xd, 0x3, 0x60, 0xd8, 0x37, 0xe, 0x43, 0xa0, 0xf4, 0x40, 0x10, 0x44, 0x31, 0x10, 0x47, 0x12, 0x84, 0xd1, 0x40, 0x53, 0x15, 0x85, 0x91, 0x68, 0x5f, 0x18, 0x86, 0x41, 0x9c, 0x6c, 0x1c, 0x7, 0x31, 0xe8, 0x7f, 0x21, 0x48, 0xa2, 0x40, 0x98, 0x27, 0xca, 0x62, 0xb8, 0xb6, 0x30, 0x8c, 0xe3, 0x60, 0xe4, 0x3b, 0xcf, 0xa4, 0x9, 0xd, 0x45, 0x11, 0xd4, 0x99, 0x2e, 0x4d, 0x93, 0xe5, 0x1d, 0x50, 0x55, 0xd6, 0x15, 0xa5, 0x69, 0x5a, 0x56, 0x95, 0xa5, 0x69 };
        public static readonly byte[] EEPROM_A2_T5_7F_E8 = new byte[] { 0x05, 0x33, 0x8c, 0xe3, 0x38, 0xce, 0x33, 0x8c, 0xe3, 0x38, 0xce, 0x33, 0x8c, 0xe3, 0x38, 0xce, 0x33, 0x8c, 0xe3, 0x38, 0xce, 0x33, 0xcc, 0xf3, 0x3c, 0xcf, 0x33, 0xcc, 0xf3, 0x40, 0xd0, 0x34, 0xd, 0x3, 0x40, 0xd0, 0x34, 0x4d, 0x13, 0x44, 0xd1, 0x34, 0x4d, 0x13, 0x48, 0xd2, 0x34, 0x8d, 0x23, 0x48, 0xd4, 0x35, 0xd, 0x43, 0x50, 0xd4, 0x35, 0xd, 0x53, 0x54, 0xd5, 0x35, 0x8d, 0x63, 0x5c, 0xd9, 0x36, 0x8d, 0xb3, 0x70, 0xdd, 0x37, 0x8d, 0xf3, 0x80, 0xe3, 0x39, 0x4e, 0x63, 0x9c, 0xe8, 0x3a, 0x4e, 0xa3, 0xac, 0xec, 0x3b, 0x4e, 0xd3, 0xbc, 0xf0, 0x3c, 0x4f, 0x23, 0xd0, 0xf6, 0x3e, 0xf, 0xa3, 0xf4, 0xfd, 0x3f, 0x4f, 0xd3, 0xf4, 0xfd };
        public static readonly byte[] EEPROM_A2_T6_7F_E8 = new byte[] { 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

        public static bool InitBobReg(YcdTelnetSecond telnet)
        {
            bool writeRes ;

            writeRes = BobBase.WriteReg(telnet, 0x51, 0x0, 0x7F, EEPROM_A2_00_7F);
            if (writeRes == false) return writeRes;
            writeRes = BobBase.WriteReg(telnet, 0x51, 0x7F, 0xFF, EEPROM_A2_T3_7F_FF);
            if (writeRes == false) return writeRes;
            writeRes = BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T4_7F_E8);
            if (writeRes == false) return writeRes;
            writeRes = BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T5_7F_E8);
            if (writeRes == false) return writeRes;
            writeRes = BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T6_7F_E8);
            if (writeRes == false) return writeRes;

            return true;
        }

        public static bool CheckSum(YcdTelnetSecond telnet)
        {
            //change table to 4
            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x03);
            byte[] table3 = BobBase.ReadReg(telnet, 0x51, 130, 210);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x04);
            byte[] table4 = BobBase.ReadReg(telnet, 0x51, 128, 232);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x05);
            byte[] table5 = BobBase.ReadReg(telnet, 0x51, 128, 232);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x06);
            byte[] table6 = BobBase.ReadReg(telnet, 0x51, 128, 232);

            //calculation check sum
            int sum = 0;
            for(int i = 0; i < table3.Length; i++)
            {
                sum += table3[i];
            }

            for(int j = 0; j < table4.Length; j++)
            {
                sum += table4[j];
                sum += table5[j];
                sum += table6[j];
            }

            bool writeCheckSum = false;

            if (sum > 0)
            {
                //write register 219
                writeCheckSum = BobBase.WriteReg(telnet, 0x51, 219, (byte)sum);
            }

            //double runseconds = (DateTime.Now - startTime).TotalSeconds;

            return writeCheckSum;
        }

        #region telnet 3
        public static bool InitBobReg(YcdTelnetThird telnet)
        {
            BobBase.WriteReg(telnet, 0x51, 0x0, 0x7F, EEPROM_A2_00_7F);

            BobBase.WriteReg(telnet, 0x51, 0x7F, 0xFF, EEPROM_A2_T3_7F_FF);

            BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T4_7F_E8);

            BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T5_7F_E8);

            BobBase.WriteReg(telnet, 0x51, 0x7F, 0xE8, EEPROM_A2_T6_7F_E8);

            return true;
        }

        public static void CheckSum(YcdTelnetThird telnet)
        {
            //change table to 4
            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x04);
            List<byte> table4 = BobBase.ReadReg(telnet, 0x51, 0x80, 0xE8);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x05);
            List<byte> table5 = BobBase.ReadReg(telnet, 0x51, 0x80, 0xE8);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x06);
            List<byte> table6 = BobBase.ReadReg(telnet, 0x51, 0x80, 0xE8);

            BobBase.WriteReg(telnet, 0x51, 0x7f, 0x03);
            List<byte> table3 = BobBase.ReadReg(telnet, 0x51, 0x82, 0xD2);

            //calculation check sum
            int sum = 0;

            for (int i = 0; i < table3.Count; i++)
            {
                sum += table3[i];
            }

            for (int j = 0; j < table4.Count; j++)
            {
                sum += table4[j];
                sum += table5[j];
                sum += table6[j];
            }

            //bool writeCheckSum = false;

            if (sum > 0)
            {
                //write register 219
                //byte db = BobBase.ReadReg(telnet, 0x51, 0xDB);
                BobBase.WriteReg(telnet, 0x51, 0xDB, (byte)sum);
            }

            //double runseconds = (DateTime.Now - startTime).TotalSeconds;

            //return writeCheckSum;
        }

        #endregion
    }
}
